package com.su.dolphin.utils.ui;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.su.dolphin.DpApp;
import com.su.dolphin.R;
import com.su.dolphin.core.AppThread;

public class TipsUtil
{
    
    /**
     * 控制Toast显示
     */
    private static boolean isShow = true;
    
    public static Dialog buildDPProgressDialog(Context context, String info) {
        Dialog progressDialog = new Dialog(context, R.style.common_dialog);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View dialogView = LayoutInflater.from(DpApp.getContext()).inflate(R.layout.layout_common_progress, null);
        if (TextUtils.isEmpty(info)) {
            ((TextView) dialogView.findViewById(R.id.txtProgressMsg)).setText("努力加载中...");
        }
        else {
            ((TextView) dialogView.findViewById(R.id.txtProgressMsg)).setText(info);
        }
        progressDialog.setContentView(dialogView);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setOnKeyListener(new OnKeyListener()
        {
            
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    dialog.dismiss();
                    return true;
                }
                return false;
            }
        });
        //progressDialog.show();
        return progressDialog;
    }
    
    /**
     * 显示退出对话框
     * 
     * @param context
     */
    public static void showExitDialog(Context context) {
        new AlertDialog.Builder(context).setMessage("确定退出吗?").setPositiveButton("是", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int whichButton) {
                // 对缓存数据进行管理
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }).setNegativeButton("是", null).show();
        
    }
    
    public static void showContextMenu(Context context, String title, CharSequence[] menus, DialogInterface.OnClickListener itemclickListener) {
        Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(title);
        dialog.setItems(menus, itemclickListener);
        dialog.show();
    }
    
    public static void showContextMenu(Context context, String title, BaseAdapter adapter, final DialogInterface.OnClickListener itemclickListener) {
        Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(title);
        dialog.setAdapter(adapter, itemclickListener);
        dialog.show();
    }
    
    public static void showConfirmDialog(Context context, CharSequence title, CharSequence message, OnClickListener listener, OnClickListener negativeClickListener) {
        Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton("确定", listener);
        dialog.setNegativeButton("取消", negativeClickListener);
        dialog.show();
    }
    
    public static void showConfirmDialog(Context context, CharSequence title, CharSequence message, OnClickListener listener) {
        showConfirmDialog(context, title, message, listener, null);
    }
    
    /**
     * 显示错误提示框
     * 
     * @param context
     * @param msg
     */
    public static void showErrorDialog(Context context, String msg) {
        new AlertDialog.Builder(context).setTitle("错误").setMessage(msg).setNegativeButton("是", null).show();
    }
    
    /**
     * 显示错误提示框
     * 
     * @param context
     * @param msg
     */
    public static void showErrorDialog(Context context, String title, String msg) {
        new AlertDialog.Builder(context).setTitle(title).setMessage(msg).setNegativeButton("是", null).show();
    }
    
    private static Toast sToast;
    
    public static void toast(String msg) {
        toast(DpApp.getContext(), msg, Toast.LENGTH_SHORT);
    }
    
    public static void toast(String msg, int time) {
        toast(DpApp.getContext(), msg, time);
    }
    
    public static void toast(int msgResId) {
        toast(DpApp.getContext(), DpApp.getContext().getString(msgResId), Toast.LENGTH_LONG);
    }
    
    public static void toast(Context context, int msgResId, int time) {
        toast(DpApp.getContext(), DpApp.getContext().getString(msgResId), time);
    }
    
    public static void toast(final Context context, final String msg, final int time) {
        AppThread.uiPost(new Runnable()
        {
            
            @Override
            public void run() {
                if (sToast != null) {
                    sToast.cancel();
                }
                sToast = Toast.makeText(context, msg, time);
                sToast.show();
            }
        });
    }
    
    /**
     * 测试用
     * 
     * @param context
     * @param msg
     */
    public static void toastTest(final String msg) {
        if (isShow) {
            toast(msg);
        }
    }
}
